package rush.rushcampus.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import rush.rushcampus.entity.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import rush.rushcampus.vo.ArticleCommentVo;
import rush.rushcampus.vo.CommentVo;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author rush
 * @since 2024-06-11
 */
@Mapper
public interface CommentMapper extends BaseMapper<Comment> {


    @Select("SELECT \n" +
            "    c.*,\n" +
            "    (\n" +
            "        SELECT GROUP_CONCAT(child_comments)\n" +
            "        FROM (\n" +
            "            SELECT \n" +
            "                ct2.parent_comment, \n" +
            "                JSON_ARRAYAGG(\n" +
            "                    JSON_OBJECT(\n" +
            "                        'id', ct2.id,\n" +
            "                        'content', ct2.content,\n" +
            "                        'userId', ct2.user_id,\n" +
            "                        'commentType', ct2.comment_type,\n" +
            "                        'addTime', ct2.add_time,\n" +
            "                        'isPrivate', ct2.is_private,\n" +
            "                        'articleId', ct2.article_id,\n" +
            "                        'parentComment', ct2.parent_comment\n" +
            "                    )\n" +
            "                ) AS child_comments\n" +
            "            FROM (\n" +
            "                WITH RECURSIVE CommentTree AS (\n" +
            "                    SELECT \n" +
            "                        c.*, \n" +
            "                        CAST(c.id AS CHAR(255)) AS path,\n" +
            "                        CAST(NULL AS CHAR(255)) as child_comments\n" +
            "                    FROM comment c\n" +
            "                    WHERE (c.parent_comment IS NULL OR c.parent_comment = '')\n" +
            "                      AND c.article_id = #{articleId}\n" +
            "                      AND c.is_private = #{isPrivate}\n" +
            "                    UNION ALL\n" +
            "                    SELECT \n" +
            "                        c.*, \n" +
            "                        CONCAT(ct.path, ',', c.id) AS path,\n" +
            "                        JSON_OBJECT(\n" +
            "                            'id', c.id,\n" +
            "                            'content', c.content,\n" +
            "                            'userId', c.user_id,\n" +
            "                            'commentType', c.comment_type,\n" +
            "                            'addTime', c.add_time,\n" +
            "                            'isPrivate', c.is_private,\n" +
            "                            'articleId', c.article_id,\n" +
            "                            'parentComment', c.parent_comment\n" +
            "                        ) AS child_comments\n" +
            "                    FROM comment c\n" +
            "                    JOIN CommentTree ct ON c.parent_comment = CAST(ct.id AS CHAR(255))\n" +
            "                    WHERE c.article_id =  #{articleId} AND c.is_private = #{isPrivate}  LIMIT #{EndPage} OFFSET #{StartPage}  -- 添加限制条件\n" +
            "                )\n" +
            "                SELECT * FROM CommentTree\n" +
            "            ) ct2\n" +
            "            GROUP BY ct2.parent_comment\n" +
            "        ) child_comments_group \n" +
            "        WHERE child_comments_group.parent_comment = CAST(c.id AS CHAR(255))\n" +
            "    )  AS childComment\n" +
            "FROM comment c\n" +
            "WHERE (c.parent_comment IS NULL OR c.parent_comment = '')\n" +
            "  AND c.article_id = #{articleId}\n" +
            "  AND c.is_private = #{isPrivate}" +
            "  ORDER BY c.add_time LIMIT #{EndPage} OFFSET #{StartPage};")
    @ResultMap("commentWithChildren") // 使用自定义结果映射
    List<CommentVo> getCommentList(Long articleId, Integer isPrivate , Integer StartPage, Integer EndPage);



//ORDER BY c.add_time LIMIT #{EndPage} OFFSET #{StartPage}
}
